package com.hfut.jpoa.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hfut.jpoa.common.dto.ResultDto;
import com.hfut.jpoa.dao.SysparaDAO;
import com.hfut.jpoa.dto.SysparaDTO;
import com.hfut.jpoa.model.Syspara;
import com.hfut.jpoa.service.SysparaService;
import com.hfut.jpoa.service.exception.SysparaException;

public class SysparaServiceImpl implements SysparaService
{
	private static final Logger LOGGER = LoggerFactory
	        .getLogger(SysparaServiceImpl.class);

	private SysparaDAO sysparaDAO;

	/** {@inheritDoc} */
	public Syspara getSyspara(long id) throws SysparaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	/** {@inheritDoc} */
	public List<Syspara> getSyspara(String key) throws SysparaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	/** {@inheritDoc} */
	public ResultDto<Syspara> querySysparaInfoForPage(SysparaDTO systemConfigDTO)
	        throws SysparaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	/** {@inheritDoc} */
	public Syspara selectSysparaInfo(Syspara record) throws SysparaException
	{

		return null;
	}

	/** {@inheritDoc} */
	public void insertSystemConfig(Syspara systemConfig)
	        throws SysparaException
	{
		sysparaDAO.insert(systemConfig);
	}

	/** {@inheritDoc} */
	public void updateSyspara(Syspara record) throws SysparaException
	{
		if (null == record || StringUtils.isBlank(record.getId()))
		{
			LOGGER.error("updateSyspara异常：值为空 ");
			throw new SysparaException("updateSyspara异常：值为空 ");
		}
		sysparaDAO.updateByPrimaryKeySelective(record);
	}

	/** {@inheritDoc} */
	public List<Syspara> selectSameSysConfig() throws SysparaException
	{
		// TODO Auto-generated method stub
		return null;
	}

	/** {@inheritDoc} */
	public void batchUpdateIndex(List<String> sameIdTypeList)
	        throws SysparaException
	{
		// TODO Auto-generated method stub
	}

	public ResultDto<Syspara> list(SysparaDTO sysparaDTO)
	        throws SysparaException
	{
		return sysparaDAO.findSysparaInfoForPage(sysparaDTO);
	}

	public ResultDto<Syspara> queryCategoryInfoForPage(SysparaDTO sysparaDTO)
	        throws SysparaException
	{

		return sysparaDAO.findSysparaInfoForPage(sysparaDTO);
	}

	public SysparaDAO getSysparaDAO()
	{
		return sysparaDAO;
	}

	public void setSysparaDAO(SysparaDAO sysparaDAO)
	{
		this.sysparaDAO = sysparaDAO;
	}

	public void saveSyspara(Syspara record) throws SysparaException
	{
		if (record == null)
		{
			LOGGER.error("插入记录异常：值为空 ");
			throw new SysparaException("插入记录异常：值为空 ");
		}
		//先判断字典表中是否已存在该字典值
		
		Syspara condition=new Syspara();
		condition.setCode(record.getCode());
		if(sysparaDAO.count(condition)==0){
			sysparaDAO.insert(record);
		}else{
			LOGGER.error("插入记录异常：字典编码已存在 ");
			throw new SysparaException("插入记录异常：字典编码已存在 ");
		}

	}

	public Syspara findSysparaInfo(Syspara record) throws SysparaException
	{
		if (null == record && StringUtils.isBlank(record.getId()))
		{
			LOGGER.error("selectSysparaInfo异常：值为空 ");
			throw new SysparaException("selectSysparaInfo异常：值为空 ");
		}
		Syspara result = sysparaDAO.selectByPrimaryKey(record.getId());

		return result;
	}

	public boolean codeValidator(Syspara syspara) throws SysparaException
	{
		if (null == syspara && StringUtils.isBlank(syspara.getCode()))
		{
			LOGGER.error("codeValidator异常：值为空 ");
			throw new SysparaException("codeValidator异常：值为空 ");
		}
		List<Syspara> lstSyspara = sysparaDAO.querySysparaList(syspara);
		if (lstSyspara != null && lstSyspara.size() > 0)
		{
			return true;
		}
		return false;
	}

	public List<Syspara> querySysparaList(Syspara syspara)
	        throws SysparaException
	{

		if (null == syspara && StringUtils.isBlank(syspara.getCode()))
		{
			LOGGER.error("querySysparaList异常：值为空 ");
			throw new SysparaException("querySysparaList异常：值为空 ");
		}
		List<Syspara> lstSyspara = sysparaDAO.querySysparaList(syspara);

		return lstSyspara;
	}

}
